//
//  SEGShareViewController.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/3/1.
//  Copyright © 2017年 陈区. All rights reserved.
//  最新的统一分享界面

#import "SEGShareViewController.h"
#import "SEGShareHelpCardView.h"

#if __has_include(<Commom/SEGAlertView.h>)
#import <Commom/SEGAlertView.h>
#else
#import "SEGAlertView.h"
#endif

@interface SEGShareViewController ()
@property (nonatomic, strong) UIImageView* maskImageView; //遮罩
@property (nonatomic, strong) UIView* planeView;
@property (nonatomic, strong) UIButton* cancelBtn;
/**
 取消按钮高度
 */
@property (nonatomic, assign) CGFloat cancelHeight;

/**
 分享互助卡片上半部分视图
 */
@property (nonatomic, strong)SEGShareHelpCardView *helpCardView;

@end

@implementation SEGShareViewController
#pragma mark - life cycle

- (instancetype)initWithViewModel:(SEGShareViewModel*)viewModel{
    if (self == [super init]) {
        _viewModel = viewModel;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    [self segInitViews];
    [self bindViewModel];
}

#pragma mark - init Views
-(void)segInitViews{
    //遮罩
    self.maskImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
    self.maskImageView.userInteractionEnabled = YES;
    self.maskImageView.backgroundColor = kColor_bg3;
    [self.view addSubview:self.maskImageView];
    
    //点击背景取消
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(seg_hiddenView)];
    [self.maskImageView addGestureRecognizer:tap];

    self.cancelHeight = 44.0 + LYSafeAreaBottom();
    
    if (self.viewModel.shareVCViewType == SEGShareVCViewType_HelpCard) {//求助卡界面单独处理
        [self seg_initShareHelpView];
    }else {
        [self seg_initPlaneView];
    }
}

- (void)seg_initShareHelpView
{
    if(self.planeView) {
        return;
    }
    
    //按钮层
    self.planeView = [[UIView alloc] init];
    self.planeView.backgroundColor = [UIColor whiteColor];
    self.planeView.layer.masksToBounds = YES;
    self.planeView.layer.cornerRadius = 5;
    [self.view addSubview:self.planeView];
    [self.planeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
        make.centerY.equalTo(self.view);
    }];
    
    //取消按钮
    self.cancelBtn = [[UIButton alloc] init];
    [self.cancelBtn setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_dialog_close_big_w")] forState:UIControlStateNormal];
    [self.cancelBtn addTarget:self action:@selector(seg_hiddenView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.cancelBtn];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(- 25 - LYSafeAreaBottom());
        make.centerX.equalTo(self.view);
    }];

    [self seg_createButtonsWithArray:self.viewModel.shareTypesArr rowIndex:0];
}

- (void)seg_initPlaneView
{
    if(self.planeView) {
        return;
    }
    
    //按钮层
    self.planeView = [[UIView alloc] init];
    self.planeView.backgroundColor = kColor_W;
    
    [self.view addSubview:self.planeView];
    [self.planeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
    }];

    //取消按钮
    self.cancelBtn = [[UIButton alloc] init];
    [self.cancelBtn setTitleColor:kColor_1 forState:UIControlStateNormal];
    self.cancelBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    self.cancelBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, LYSafeAreaBottom(), 0);//适配iPhoneX 按钮 字向上偏移安全区域底部的距离
    [self.cancelBtn addTarget:self action:@selector(seg_hiddenView) forControlEvents:UIControlEventTouchUpInside];
    [self.planeView addSubview:self.cancelBtn];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.planeView);
        make.height.mas_equalTo(self.cancelHeight);
        make.bottom.equalTo(self.planeView);
    }];
    
    if (self.viewModel.shareVCViewType == SEGShareVCViewType_Pictorial) {//画报界面单独处理
        //标题
        UILabel *label = [[UILabel alloc] init];
        label.textColor = kColor_1;
        label.font = [UIFont systemFontOfSize:13];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"有趣的内容要和朋友一起分享！";
        [self.planeView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.planeView);
            make.top.equalTo(self.planeView).offset(24.0);
        }];
        
        [self.cancelBtn setTitle:@"暂时不" forState:UIControlStateNormal];
        [self seg_createButtonsWithArray:self.viewModel.shareTypesArr rowIndex:0];
    }else {
        [self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];

        if (self.viewModel.shareTypesArr && self.viewModel.shareTypesArr.count > 0) {
            [self seg_createButtonsWithArray:self.viewModel.shareTypesArr rowIndex:0];
            if (self.viewModel.otherActionTypesArr.count > 0) {
                [self seg_createButtonsWithArray:self.viewModel.otherActionTypesArr rowIndex:1];
            }
        }else {
            if (self.viewModel.otherActionTypesArr.count > 0) {
                [self seg_createButtonsWithArray:self.viewModel.otherActionTypesArr rowIndex:0];
            }
        }
    }
}

- (void)seg_hiddenView
{
    [UIView animateWithDuration:0.2 animations:^{
        self.maskImageView.alpha = 0;
        self.planeView.alpha = 0;
    }
     completion:^(BOOL finished) {
         [self.maskImageView removeFromSuperview];
         [self.planeView removeFromSuperview];
         self.maskImageView = nil;
         WeakSelf;
         [self dismissViewControllerAnimated:YES completion:^{
             if (weakSelf.dismissBlock) weakSelf.dismissBlock();
         }];

     }];
}

- (void)seg_createButtonsWithArray:(NSArray *)array rowIndex:(NSUInteger)rowIndex
{
    CGFloat rowHeight = 74.0;
    CGFloat iconWidth = 30.0;
    CGFloat iconPadding = 44.0;//图标间距
    CGFloat xPadding = 40.0;
    CGFloat yPadding = 13.0 + rowHeight * rowIndex;
    CGFloat planViewHeight;
    NSInteger count = self.viewModel.shareTypesArr.count;
    if (self.viewModel.shareVCViewType == SEGShareVCViewType_Pictorial) {//画报界面单独处理
        rowHeight = 153.0;
        xPadding = 40.0;
        yPadding = 74.0 + rowHeight * rowIndex;
        NSInteger count = self.viewModel.shareTypesArr.count;
        if (count == 1) {
            iconPadding = 0;
        }else {
            iconPadding = (kScreenWidth - xPadding * 2 - (iconWidth * count)) / (count - 1);
        }
    }
    
    if (self.viewModel.shareVCViewType == SEGShareVCViewType_HelpCard) {//求助卡界面
        self.helpCardView.hidden = NO;
        
        //顶部分享互助内容高度
        CGFloat viewHeight = self.helpCardView.viewHeight;
        rowHeight = 68.0;
        xPadding = 41.5;
        
        if (count == 1) {
            iconPadding = 0;
        }else {
            iconPadding = (kScreenWidth - 30 - xPadding * 2 - (iconWidth * count) ) / (count - 1);
        }
        yPadding = viewHeight + rowHeight * rowIndex;
        
        planViewHeight = (rowIndex + 1) * rowHeight + viewHeight;
        
    }else {
        //分隔线
        UIView* lineView = [[UIView alloc] init];
        lineView.backgroundColor = kColor_L2;
        [self.planeView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.planeView);
            make.height.mas_equalTo(0.5);
            make.top.equalTo(self.planeView).offset(rowHeight * (rowIndex + 1));
        }];
        
        planViewHeight = (rowIndex + 1) * rowHeight + self.cancelHeight;
    }
    
    //调整背景视图高度
    [self.planeView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(planViewHeight);
    }];
    
    
    // 创建按钮
    for (int i = 0; i < array.count; i++) {
        NSNumber *type = array[i];
        
        UIButton* button = [[UIButton alloc] init];
        [self.planeView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
             make.left.equalTo(self.planeView).offset(xPadding + (iconWidth + iconPadding) * i);
             make.top.equalTo(self.planeView).with.offset(yPadding);
             make.width.height.mas_equalTo(iconWidth);
        }];
        
        UILabel *label = [[UILabel alloc] init];
        label.textColor = kColor_1;
        if (self.viewModel.shareVCViewType == SEGShareVCViewType_HelpCard) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
        }
        label.font = [UIFont systemFontOfSize:10];
        label.textAlignment = NSTextAlignmentCenter;
        [self.planeView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(button);
            make.top.equalTo(button.mas_bottom).offset(7);
        }];
        
        SEGShareVCActionType actionType = [type integerValue];
        button.tag = actionType;
        [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [self.viewModel segClickSharedChannelButton:button.tag cardImage:[self.helpCardView generateShareHelpCardImage]];
            [self seg_hiddenView];
        }];
        
        switch (actionType) {
            case SEGShareVCActionType_Share_WeChat: //微信
            case SEGShareVCActionType_MiniProgram_WeChat: // 微信小程序
            {
                [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_share_wechat")] forState:UIControlStateNormal];
                button.imageView.image = [button.imageView.image imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
                button.imageView.tintColor = kColor_1;
                label.text = @"微信";
                if (self.viewModel.shareVCViewType == SEGShareVCViewType_HelpCard) {
                    button.imageView.tintColor = [UIColor colorWithHexString:@"#333333"];
                }
                break;
            }
            case SEGShareVCActionType_Share_FriendCircle: //朋友圈
            {
                [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_share_pengyouquan")] forState:UIControlStateNormal];
                button.imageView.image = [button.imageView.image imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
                button.imageView.tintColor = kColor_1;
                label.text = @"朋友圈";
                if (self.viewModel.shareVCViewType == SEGShareVCViewType_HelpCard) {
                    button.imageView.tintColor = [UIColor colorWithHexString:@"#333333"];
                }
                break;
            }
            case SEGShareVCActionType_Share_QQ: //QQ
            {
                [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_share_qq")] forState:UIControlStateNormal];
                button.imageView.image = [button.imageView.image imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
                button.imageView.tintColor = kColor_1;
                label.text = @"QQ";
                if (self.viewModel.shareVCViewType == SEGShareVCViewType_HelpCard) {
                    button.imageView.tintColor = [UIColor colorWithHexString:@"#333333"];
                }
                break;
            }
            case SEGShareVCActionType_Share_Neighbourhood: //邻里圈
            {
                [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_share_linliquan")] forState:UIControlStateNormal];
                button.imageView.image = [button.imageView.image imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
                button.imageView.tintColor = kColor_1;
                label.text = @"邻里圈";
                if (self.viewModel.shareVCViewType == SEGShareVCViewType_HelpCard) {
                    button.imageView.tintColor = [UIColor colorWithHexString:@"#333333"];
                }
                break;
            }
            case SEGShareVCActionType_Share_Message: //短信
            {
                [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_share_message")] forState:UIControlStateNormal];
                button.imageView.image = [button.imageView.image imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
                button.imageView.tintColor = kColor_1;
                label.text = @"短信";
                if (self.viewModel.shareVCViewType == SEGShareVCViewType_HelpCard) {
                    button.imageView.tintColor = [UIColor colorWithHexString:@"#333333"];
                }
                break;
            }
            case SEGShareVCActionType_Delete: //删除
            {
                [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_share_del")] forState:UIControlStateNormal];
                button.imageView.image = [button.imageView.image imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
                button.imageView.tintColor = kColor_1;
                label.text = @"删除";
                if (self.viewModel.shareVCViewType == SEGShareVCViewType_HelpCard) {
                    button.imageView.tintColor = [UIColor colorWithHexString:@"#333333"];
                }
                break;
            }
            case SEGShareVCActionType_Again: //重新上架
            {
                [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_share_again")] forState:UIControlStateNormal];
                button.imageView.image = [button.imageView.image imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
                button.imageView.tintColor = kColor_1;
                label.text = @"重新上架";
                if (self.viewModel.shareVCViewType == SEGShareVCViewType_HelpCard) {
                    button.imageView.tintColor = [UIColor colorWithHexString:@"#333333"];
                }
                break;
            }
            case SEGShareVCActionType_Edit: //编辑
            {
                [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_share_edit")] forState:UIControlStateNormal];
                button.imageView.image = [button.imageView.image imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
                button.imageView.tintColor = kColor_1;
                label.text = @"编辑";
                if (self.viewModel.shareVCViewType == SEGShareVCViewType_HelpCard) {
                    button.imageView.tintColor = [UIColor colorWithHexString:@"#333333"];
                }
                break;
            }
            case SEGShareVCActionType_SellOut: //已出手
            {
                [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_share_sellout")] forState:UIControlStateNormal];
                button.imageView.image = [button.imageView.image imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
                button.imageView.tintColor = kColor_1;
                label.text = @"已出手";
                if (self.viewModel.shareVCViewType == SEGShareVCViewType_HelpCard) {
                    button.imageView.tintColor = [UIColor colorWithHexString:@"#333333"];
                }
                break;
            }
            case SEGShareVCActionType_Report: //举报
            {
                [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_share_report")] forState:UIControlStateNormal];
                button.imageView.image = [button.imageView.image imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
                button.imageView.tintColor = kColor_1;
                label.text = @"举报";
                if (self.viewModel.shareVCViewType == SEGShareVCViewType_HelpCard) {
                    button.imageView.tintColor = [UIColor colorWithHexString:@"#333333"];
                }
                break;
            }
            case SEGShareVCActionType_NotIntersting: //不感兴趣
            {
                [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_share_nointerest")] forState:UIControlStateNormal];
                button.imageView.image = [button.imageView.image imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
                button.imageView.tintColor = kColor_1;
                label.text = @"不感兴趣";
                if (self.viewModel.shareVCViewType == SEGShareVCViewType_HelpCard) {
                    button.imageView.tintColor = [UIColor colorWithHexString:@"#333333"];
                }
                break;
            }
        }
    }
    
}

#pragma mark - Layout
- (void)updateViewConstraints {
    [super updateViewConstraints];
}
#pragma mark - Private Methods

#pragma mark - RAC Data Binding
- (void)bindViewModel {
    [self.viewModel.messageSubject subscribeNext:^(NSString*  _Nullable x) {
        [SEGAlertView showTmepMessageWithMessage:x];
    }];
    
    [self.viewModel.sharedButtonClickSubject subscribeNext:^(NSDictionary*  _Nullable x) {
        
        if ([[x valueForKey:@"type"] isEqualToString:@"1"]) {
            if (self.delegate && [self.delegate respondsToSelector:@selector(shareVCActionButtonPressedWithChannel:)]) {
                [self.delegate shareVCActionButtonPressedWithChannel:self.viewModel.channel];
            }
            if (self.channelBlock) {
                self.channelBlock(self.viewModel.channel);
            }
        }
        if ([[x valueForKey:@"type"] isEqualToString:@"2"]) {
            NSInteger actionType = [[x valueForKey:@"value"] integerValue];
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(shareVCActionButtonPressedWithType:)]) {
                [self.delegate shareVCActionButtonPressedWithType:actionType];
            }
            if (self.actionTypeBlock) {
                self.actionTypeBlock(actionType);
            }
        }
        
        // [self seg_hiddenView];
    }];
}

#pragma mark - getter
- (SEGShareHelpCardView *)helpCardView
{
    if (!_helpCardView) {
        _helpCardView = [[SEGShareHelpCardView alloc] init];
        [self.planeView addSubview:_helpCardView];
        [_helpCardView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self.planeView);
        }];
        _helpCardView.helpCardModel = self.viewModel.helpCardModel;
        [_helpCardView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(_helpCardView.viewHeight);
        }];
    }
    return _helpCardView;
}

-(SEGShareViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[SEGShareViewModel alloc]init];
    }
    return _viewModel;
}
@end
